package com.zstu.lvxiang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zstu.lvxiang.mapper.StudentMapper;
import com.zstu.lvxiang.pojo.Student;
import com.zstu.lvxiang.service.IStudentService;
import com.zstu.lvxiang.utils.DateUtils;
import com.zstu.lvxiang.utils.MD5Utils;
import com.zstu.lvxiang.utils.UUIDUtils;
import com.zstu.lvxiang.vo.LoginVo;
import com.zstu.lvxiang.vo.RespBean;
import com.zstu.lvxiang.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Trafalgar
 * @since 2022-03-16
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletResponse response) {
        Student student = studentMapper.selectBySno(loginVo.getSno());
        if (null == student) {
            return RespBean.error(RespBeanEnum.SESSION_ERROR);
        }
        if (!MD5Utils.inputPassToFromPass(loginVo.getPassword()).equals(student.getPassword())) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        String uuid = UUIDUtils.uuid();
        redisTemplate.opsForValue().set("report_sys_sno:" + uuid, student);
        Cookie cookie = new Cookie("sno", uuid);
        cookie.setPath("/");
        response.addCookie(cookie);
        return RespBean.success(student);
    }

    @Override
    public Student getStuByCookie(String session,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        return (Student) redisTemplate.opsForValue().get("report_sys_sno:" + session);
    }

    @Override
    public RespBean punch(String sno) {
        Student student = studentMapper.selectBySno(sno);
        Date last = student.getLastPunchTime();
        Date sys = new Date();
        String sysTime = DateUtils.dateToString(sys, DateUtils.DEFAULT_DATE_PATTERN);
        String lastTime = DateUtils.dateToString(last, DateUtils.DEFAULT_DATE_PATTERN);
        long diffSignDay = DateUtils.dayDiff(sysTime, lastTime, DateUtils.DEFAULT_DATE_PATTERN);
        if (diffSignDay > 1) {
            if (studentMapper.restart(sno, sys) > 0) {
                return RespBean.success(1);
            }
        } else if (diffSignDay == 0) {
            return RespBean.error(RespBeanEnum.PUNCH_ERROR);
        } else {
            if (studentMapper.punch(sno, sys) > 0) {
                return RespBean.success(student.getPunch() + 1);
            }
        }
        return RespBean.error(RespBeanEnum.ERROR);
    }


}
